/**
 * 管理婚庆活动
 * @type {{}}
 */
var SysParty = {
    page_limit:20,  // 每页显示数
    page_current:1  //当前页数
};
//初始化
SysParty.init = function()
{
    var _self = this;
    _self.page_current = 1;
    _self.page_limit = 20;
    $.ajax({
        type:'GET',
        url:APP+'/party/getParty.html',
        success:function(count){
            var page = ['<li class="arrow unavailable"><a href="javascript:;">&laquo;</a></li> '],
                pages = (parseInt(count)/_self.page_limit).toFixed(0);
            for(var i=1;i<parseInt(pages)+2;i++){
                page.push('<li '+(i == 1?'class="current"':'')+'>' +
                          '<a href="javascript:;" onclick="SysParty.page('+i+')">'+i+'</a>' +
                          '</li>');
            }
            page.push('<li class="arrow '+(pages==0?"unavailable":"")+'" ><a href="javacript:;">&raquo;</a></li>');
            $('#partyPage').html(page.join(''));
            if(pages>0){

            }
        }
    });
    _self.page(_self.page_current);
}
SysParty.page = function(i)
{
    var _self = this,
        lis = $('#partyPage li');
    _self.page_current = i;
    lis.removeClass('unavailable').removeClass('current');
    if(i == 1){
        lis.first().addClass('unavailable').children('a').unbind('click');
    }else if(i = lis.length-2){
        lis.last().addClass('unavailable').children('a').unbind('click');
    }else{
        lis.first().children('a').unbind('click').bind('click',function(){
            _self.page(_self.page_current -1);
        });
        lis.last().children('a').unbind('click').bind('click',function(){
            _self.page(_self.page_current -1);
        });
        list.eq(i-1).addClass('current');
    }
    $.ajax({
        type: "GET",
        url:APP+'/party/getParty.html',
        data:'page='+_self.page_current+"&limit="+_self.page_limit,
        success:function(data){
            _self.callbackTable(data);
        },
        error:function(e){
            Public.popup('活动数据加载失败:'+e);
        },
        dataType:'json'
    });
}
//绘制表格
SysParty.callbackTable = function(data)
{
    var tb = "",
        tbObj = $('#party-table tbody'),
        _self = this;
    if(data != null && data.length > 0){
        var total = (_self.page_current - 1)*_self.page_limit;
        //_self.page_current++;
        $.each(data,function(k,v){
            tb += "<tr><td>"+ (total+k+1)+"</td>" +
                  "<td>"+ v.man+"</td><td>"+ v.woman+"</td>" +
                  "<td>"+ v.begin+" 至 "+ v.end+"</td>" +
                  "<td>"+ (v.images!=null?v.images.length:0)+"</td>" +
                  "<td><a class='button tiny alert' href='javascript:;' onclick='SysParty.deletePart("+ v.id+")'>删除</a>"+
                  "<a class='button tiny alert' href='javascript:;' onclick='SysParty.detailParty("+ v.id+")'>查看</a></td></tr>";
        });
    }else{
        tb = '<tr><td colspan="6">暂无数据</td></tr>';
    }
    tbObj.html(tb);
}
//现实添加活动表单
SysParty.form = function(type)
{
    var f = $('#partyForm');
    if(f.css('display') == 'none') f.fadeIn();
    if(type == 'update'){

    }else{

    }
}
//文件上传事件
SysParty.upload = function()
{
    var man = $('#rightManName').val(),
        woman = $('#rightWomanName').val(),
        begin = $('#beginTime').val(),
        end = $('#endTime').val(),
        _self = this;
    if(!this.validUpload(man,woman,begin,end)) return false;
    $('#partyCreateForm').ajaxForm({
       // target:'#target',
        error:function(e){
            Public.popup('活动保存出错:'+e);
        },
        success:function(e){
           if(e=='null'){
               $('#partyForm').fadeOut();
               Public.popup('活动保存成功！');
               _self.page(_self.page_current);
           }else{
               $('#party-alert').html(e).fadeIn().effect('shake');
           }
        }
    }).submit();
}
//验证表单输入
SysParty.validUpload = function(man,woman,begin,end)
{
    var o = $('#party-alert');
    if(man==""){
        o.html('请输入新郎姓名').fadeIn().effect('shake');
        return false;
    }
    if(woman==""){
        o.html('请输入新娘姓名').fadeIn().effect('shake');
        return false;
    }
    if(begin==""){
        o.html('请选择婚庆开始时间').fadeIn().effect('shake');
        return false;
    }
    if(end==""){
        o.html('请选择婚庆结束时间').fadeIn().effect('shake');
        return false;
    }
    return true;
}
//增加文件上传按钮
SysParty.addImage = function()
{
    var input = '<div class="fileItem">'+
                '<input name="image[]" type="file"/>'+
                '<a href="javascript:;" class="button tiny alert" onclick="SysParty.removeImage(this);">删除</a>'+
                '</div>';
    $('#imageBox_1').append(input);
}
//移除要添加的图片
SysParty.removeImage = function(obj)
{
    var o = $(obj),
        left = o.prev('input[type=file]');
    o.remove();
    left.remove();
}
//删除活动
SysParty.deletePart = function(id)
{
    if(confirm("确认要删除此活动吗？")){
        var _self = this;
        $.ajax({
            type:'POST',
            url:APP+'/party/deleteParty',
            data:'id='+id ,
            success:function(e){
                if(e == 'null'){
                    Public.popup('活动删除成功！');
                    _self.page(_self.page_current);
                }else{
                    Public.popup('删除失败：'+e);
                }
            }
        });
    }

}

SysParty.detailParty = function(id){
    location.href = APP + "/party/merry.html?id="+ id;
}